//app.js
import {
    ToastPannel
} from './component/toast/toast'
const config = require('./config.js');
const util = require('./utils/util.js');
import { getMemberInfo} from './static/api/api.js'
App({
    ToastPannel,
    onLaunch: function() {
        let that = this;
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || []
        that.getSysInfo()
        
    },
    onShow:function(options){
        let that = this;
        let params = decodeURIComponent(options.query.scene || '');
        console.log('二维码参数')
        console.log(params)
        //获取拓展码
        that.globalData.promote_store_id = params;  
        that.getUserInfo()
        wx.setStorageSync('is_author', false)
        
        
    },
    onShareAppMessage: function () {
        let that = this;
        return {
            title: '美帮在线客户端', // 转发后 所显示的title
            path: '/pages/author_page/author_page', // 相对的路径
            success: (res) => {    // 成功后要做的事情
                console.log(res.shareTickets[0])
                // console.log

                wx.getShareInfo({
                    shareTicket: res.shareTickets[0],
                    success: (res) => {
                        // that.setData({
                        //     isShow: true
                        // })
                        // console.log(that.setData.isShow)
                    },
                    fail: function (res) { console.log(res) },
                    complete: function (res) { console.log(res) }
                })
            },
            fail: function (res) {
                // 分享失败
                console.log(res)
            }
        }
    },
    getUserInfo: function(cb) {
        var that = this
        let flag = false;
        if (wx.getStorageSync('token')) flag = true;
        if (that.globalData.promote_store_id) flag = false;
        console.log(flag)
        if (flag) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //调用登录接口
            wx.login({
                success: function(ev) {
                    console.log('登录成功')
                    console.log(ev)
                    that.globalData.ev = ev;
                    wx.getUserInfo({
                        withCredentials: true,
                        success: function(res) {
                            console.log('获取用户信息unionid')
                            console.log(res);
                            wx.request({
                                url: config.openIdUrl,
                                method: 'POST',
                                header: {
                                    'site': config.site,
                                    'os': config.os,
                                    'token': wx.getStorageSync('token'),
                                    type:1
                                },
                                data: {
                                    code: ev.code,
                                    promote_code:that.globalData.promote_code,
                                    encryptedData: res.encryptedData,
                                    iv: res.iv,
                                    signature: res.signature,
                                    rawData: res.rawData,
                                    store_id: that.globalData.promote_store_id
                                    
                                },
                                success: function(res) {
                                    if (res.data.code == '2') {

                                        wx.showModal({
                                            title: '温馨提示',
                                            content: res.data.msg,
                                        })
                                    }
                                    wx.setStorageSync('token', res.data.data.session_key)
                                    wx.setStorageSync('unionid', res.data.data.unionid)
                                    if(typeof cb == 'function'){
                                        cb(res.data.data)
                                    }
                                   
                                }
                            })
                        },
                        fail: function() {
                            // wx.showModal({
                            //     title: '警告',
                            //     content: '尚未进行授权，请点击确定跳转到授权页面进行授权。',
                            //     success: function (res) {
                            //         if (res.confirm) {
                            //             console.log('用户点击确定')
                            //             wx.navigateTo({
                            //                 url: '/pages/author_login/author_login',
                            //             })
                            //         }
                            //     }
                            // })
                        }
                    })
                }
            })
        }
    },
    //获取用户信息
    _getUserInfo(cb) {
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        getMemberInfo().then(response=>{
            console.log('请求成功')
            wx.hideLoading();
            that.globalData.memberInfo = response.data;
            
            //未绑定门店
            if (!response.data.store_id || response.data.store_id == 0 || response.data.is_expire==2){
                wx.redirectTo({
                    url: '/pages/open_shop/open_shop',
                })
                return;
            }
            //已申请开店，未通过审核
            if (response.data.store_status == '-3' || response.data.store_status == '-1') {
                wx.redirectTo({
                    url: '/pages/open_shop/open_shop',
                })
                return;
            }
            //手艺人角色
            if (response.data.store_type == '20' || (response.data.store_type == '10' && response.data.is_manager!=1)) {
                wx.redirectTo({
                    url: "/pages/craftsman_page/craftsman_page",
                })
                return;
            }
            //店铺角色
            if (response.data.is_manager == 1) {
                wx.reLaunch({
                    url: "/pages/shop_index/shop_index",
                })
                return;
            }
            //服务到期
            if (response.data.is_expire == 1) {
                wx.redirectTo({
                    url: '/pages/time_out/time_out',
                })
                return;
            }
            typeof cb == "function" && cb(response.data)
        }).catch(response=>{
            wx.hideLoading();
            console.log(response)
        })
        
    },
    //获取窗口高度
    getSysInfo(){
        var that = this
        // 获取系统信息
        wx.getSystemInfo({
            success: function (res) {
                that.globalData.sysInfo = res;
            }
        })
    },

    
    
    globalData: {
        storeId: '',
        userInfo: null,
        memberInfo:null,
        sysInfo:null,
        promote_code:null,
        promote_store_id:'',
        ev:null,
        WebSiteConfig:null,
    }
})